package cn.lystudio.rohm_time_op.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.lystudio.rohm_time_op.entity.dto.UsmBtRecordDTO;
import cn.lystudio.rohm_time_op.entity.dto.UsmCdRecordDTO;
import cn.lystudio.rohm_time_op.entity.po.UsersDepartmentPO;
import cn.lystudio.rohm_time_op.entity.po.UsersPO;
import cn.lystudio.rohm_time_op.entity.po.UsmBtRecordPO;
import cn.lystudio.rohm_time_op.entity.po.UsmCdRecordPO;
import cn.lystudio.rohm_time_op.mapper.UsersDepartmentPOMapper;
import cn.lystudio.rohm_time_op.mapper.UsersPOMapper;
import cn.lystudio.rohm_time_op.mapper.UsmCdRecordPOMapper;
import cn.lystudio.rohm_time_op.service.UsmCdRecordService;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
@Slf4j
public class UsmCdRecordServiceImpl implements UsmCdRecordService {
    @Resource
    private UsmCdRecordPOMapper usmCdRecordPOMapper;
    @Resource
    private UsersDepartmentPOMapper usersDepartmentPOMapper;
    @Resource
    private UsersPOMapper usersPOMapper;

    @Override
    @DSTransactional
    public int addRecord(UsmCdRecordPO usmCdRecordPO) {
        LambdaQueryWrapper<UsmCdRecordPO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UsmCdRecordPO::getMachineNo,usmCdRecordPO.getMachineNo())
                .eq(UsmCdRecordPO::getLotEndFlag,false)
                .orderByDesc(UsmCdRecordPO::getRecordTimeDT)
                .last("LIMIT 1");
        UsmCdRecordPO record0 = usmCdRecordPOMapper.selectOne(wrapper);
        if(ObjectUtil.isEmpty(record0)) return usmCdRecordPOMapper.insert(usmCdRecordPO);
        usmCdRecordPO.setRecordID(record0.getRecordID());
        return usmCdRecordPOMapper.updateById(usmCdRecordPO);
    }

    @Override
    public List<UsmCdRecordDTO> getList(String selectedMachineNo, String selectedDateFrom,String selectedDateTo) {
        //1.先查找半田的用户与department
        List<UsersDepartmentPO> usersDepartmentPOList = usersDepartmentPOMapper.selectList(new LambdaQueryWrapper<UsersDepartmentPO>().eq(UsersDepartmentPO::getTopDeptID, 147));
        if(CollUtil.isEmpty(usersDepartmentPOList)) return null;
        List<Integer> userIDList =CollUtil.list(false);
        CollUtil.forEach(usersDepartmentPOList.iterator(),(item,index)->{
            userIDList.add(item.getUserID());
        });
        //2.查找用户
        List<UsersPO> usersPOList = usersPOMapper.selectList(new LambdaQueryWrapper<UsersPO>().in(UsersPO::getUserID, userIDList));
        if(CollUtil.isEmpty(usersPOList)) return null;
        //3.查找数据
        List<UsmCdRecordPO> usmCdRecordList = usmCdRecordPOMapper.selectList(new LambdaQueryWrapper<UsmCdRecordPO>().eq(UsmCdRecordPO::getMachineNo, selectedMachineNo).between(UsmCdRecordPO::getRecordTimeDT, selectedDateFrom,selectedDateTo));
        if(CollUtil.isEmpty(usmCdRecordList)) return  null;
        //4.组装view层数据
        List<UsmCdRecordDTO> usmCdRecordDTOList= CollUtil.list(false);
        CollUtil.forEach(usmCdRecordList.iterator(),(usmCdRecord,index)->{
            UsmCdRecordDTO usmCdRecordDTO = new UsmCdRecordDTO();
            usmCdRecordDTO.setUsmCdRecord(usmCdRecord);
            if(StrUtil.isNotEmpty(usmCdRecord.getUserCode1())){
                usmCdRecordDTO.setUser1(
                        CollUtil.findOne(usersPOList,(user)->{
                            return user.getUserCode().equals(usmCdRecord.getUserCode1());
                        })
                );
            }
            if(StrUtil.isNotEmpty(usmCdRecord.getUserCode2())){
                usmCdRecordDTO.setUser2(
                        CollUtil.findOne(usersPOList,(user)->{
                            return user.getUserCode().equals(usmCdRecord.getUserCode2());
                        })
                );
            }
            if(StrUtil.isNotEmpty(usmCdRecord.getUserCode3())){
                usmCdRecordDTO.setUser3(
                        CollUtil.findOne(usersPOList,(user)->{
                            return user.getUserCode().equals(usmCdRecord.getUserCode3());
                        })
                );
            }
            if(StrUtil.isNotEmpty(usmCdRecord.getUserCode4())){
                usmCdRecordDTO.setUser4(
                        CollUtil.findOne(usersPOList,(user)->{
                            return user.getUserCode().equals(usmCdRecord.getUserCode4());
                        })
                );
            }
            usmCdRecordDTOList.add(usmCdRecordDTO);
        });
        return usmCdRecordDTOList;
    }

    @Override
    public UsmCdRecordPO getRecordUnfinished(UsmCdRecordPO usmCdRecordPO) {
        LambdaQueryWrapper<UsmCdRecordPO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UsmCdRecordPO::getMachineNo,usmCdRecordPO.getMachineNo())
                .eq(UsmCdRecordPO::getLotEndFlag,false)
                .orderByDesc(UsmCdRecordPO::getRecordTimeDT)
                .last("LIMIT 1");
        return usmCdRecordPOMapper.selectOne(wrapper);
    }

    @Override
    public List<UsmCdRecordDTO> getFinishedRecordByLotNo(String lotNo) {
        //1.先查找半田的用户与department
        List<UsersDepartmentPO> usersDepartmentPOList = usersDepartmentPOMapper.selectList(new LambdaQueryWrapper<UsersDepartmentPO>().eq(UsersDepartmentPO::getTopDeptID, 147));
        if(CollUtil.isEmpty(usersDepartmentPOList)) return null;
        List<Integer> userIDList =CollUtil.list(false);
        CollUtil.forEach(usersDepartmentPOList.iterator(),(item,index)->{
            userIDList.add(item.getUserID());
        });
        //2.查找用户
        List<UsersPO> usersPOList = usersPOMapper.selectList(new LambdaQueryWrapper<UsersPO>().in(UsersPO::getUserID, userIDList));
        if(CollUtil.isEmpty(usersPOList)) return null;
        //3.查找数据
        LambdaQueryWrapper<UsmCdRecordPO> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(UsmCdRecordPO::getLotNo,lotNo)
                .eq(UsmCdRecordPO::getLotEndFlag,true);
        List<UsmCdRecordPO> usmCdRecordList = usmCdRecordPOMapper.selectList(wrapper);
        if(CollUtil.isEmpty(usmCdRecordList)) return null;
        //4.组装view层数据
        List<UsmCdRecordDTO> usmCdRecordDTOList= CollUtil.list(false);
        CollUtil.forEach(usmCdRecordList.iterator(),(usmCdRecord,index)->{
            UsmCdRecordDTO usmCdRecordDTO = new UsmCdRecordDTO();
            usmCdRecordDTO.setUsmCdRecord(usmCdRecord);
            if(StrUtil.isNotEmpty(usmCdRecord.getUserCode1())){
                usmCdRecordDTO.setUser1(
                        CollUtil.findOne(usersPOList,(user)->{
                            return user.getUserCode().equals(usmCdRecord.getUserCode1());
                        })
                );
            }
            if(StrUtil.isNotEmpty(usmCdRecord.getUserCode2())){
                usmCdRecordDTO.setUser2(
                        CollUtil.findOne(usersPOList,(user)->{
                            return user.getUserCode().equals(usmCdRecord.getUserCode2());
                        })
                );
            }
            if(StrUtil.isNotEmpty(usmCdRecord.getUserCode3())){
                usmCdRecordDTO.setUser3(
                        CollUtil.findOne(usersPOList,(user)->{
                            return user.getUserCode().equals(usmCdRecord.getUserCode3());
                        })
                );
            }
            if(StrUtil.isNotEmpty(usmCdRecord.getUserCode4())){
                usmCdRecordDTO.setUser4(
                        CollUtil.findOne(usersPOList,(user)->{
                            return user.getUserCode().equals(usmCdRecord.getUserCode4());
                        })
                );
            }
            usmCdRecordDTOList.add(usmCdRecordDTO);
        });
        return usmCdRecordDTOList;

    }

    @Override
    public UsmCdRecordPO getUsmCdRecordByID(String recordID) {
        LambdaQueryWrapper<UsmCdRecordPO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UsmCdRecordPO::getRecordID,recordID)
                .eq(UsmCdRecordPO::getLotEndFlag,true)
                .last("LIMIT 1");
        return usmCdRecordPOMapper.selectOne(wrapper);
    }

    @Override
    public int uptRecord(UsmCdRecordPO usmCdRecordPO) {
        return usmCdRecordPOMapper.updateById(usmCdRecordPO);
    }

    @Override
    public int delRecord(Integer recordID) {
        return usmCdRecordPOMapper.deleteById(recordID);
    }
}
